/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidate.ejb;

import entities.Candidate;
import entities.PasswordRecovery;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataSource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.TransactionPhase;
import javax.inject.Inject;
import javax.inject.Named;
import mail.EmailEJB;
import mail.EmailTemplateHelper;
import static mail.EmailTemplateHelper.facesSearchId;
import static mail.EmailTemplateHelper.getHtmlHeader;
import static mail.EmailTemplateHelper.getSocialNetworkFooter;
import static mail.EmailTemplateHelper.logoId;
import mail.datamodel.IdDataSource;
import qualifiers.CreatedEntityEvent;
import scheduling.qualifiers.RememberCandidateMailEvent;

/**
 *
 * @author Alvaro
 */
@Named
@ApplicationScoped
public class MailCandidateEjb implements Serializable{
    @Inject
    EmailEJB emailEJB;
    private static final String logoRelativePath="resources/emailImages/emailLogo.png";
    private static final String facesRelativePath="resources/emailImages/emailFaces.png";
    
    public void onNewCandidate(@Observes(during = TransactionPhase.AFTER_SUCCESS) @CreatedEntityEvent Candidate c)throws IOException, URISyntaxException{
        String subject=EmailTemplateHelper.getMessages().getString("WelcomeToTheRecruitery");
        String html=EmailTemplateHelper.getHtmlHeader()+EmailTemplateHelper.getHtmlFacesBody()+getHtmlWelcomeBody()+getHtmlUserCredentials(c.getUser().getEmail(), c.getUser().getPassword())+getSocialNetworkFooter();
        List<IdDataSource>  dataSources=new ArrayList<IdDataSource>();
        dataSources.add(new IdDataSource(getLogoDataSource(), EmailTemplateHelper.logoId));
        dataSources.add(new IdDataSource(getFacesDataSource(), EmailTemplateHelper.facesId));
        emailEJB.sendHtmlMail(c.getUser().getEmail(), subject, html, dataSources);
    }
    
    public void onNewRecovery(@Observes(during = TransactionPhase.AFTER_SUCCESS) @CreatedEntityEvent PasswordRecovery passwordRecovery)throws IOException, URISyntaxException{
        String subject=EmailTemplateHelper.getMessages().getString("PasswordRecoverySubject");
        StringBuilder sb=new StringBuilder();
        sb.append(EmailTemplateHelper.getMessages().getString("PasswordRecoveryText"));
        sb.append(" ");
        sb.append("http://www.therecruitery.com/views/recover/Recover.xhtml?id=");
        sb.append(passwordRecovery.getIdPasswordRecovery());
        String html=EmailTemplateHelper.getHtmlHeader()+EmailTemplateHelper.getHtmlDivText(sb.toString());
        List<IdDataSource> dataSources=new ArrayList<IdDataSource>();
        dataSources.add(new IdDataSource(getLogoDataSource(), logoId));
        emailEJB.sendHtmlMail(passwordRecovery.getUser().getEmail(), subject, html, dataSources);
    }
    
    public void onRememberNoPictures(@Observes @RememberCandidateMailEvent Candidate candidate) throws IOException, URISyntaxException{
        Properties properties = new Properties();
        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("messages.properties"));
        String subject=properties.getProperty("NoPicturesRememberSubject");
        String html=getHtmlHeader()+getHtmlFacesBody()+EmailTemplateHelper.getHtmlDivText(properties.getProperty("NoPicturesRememberText1"))+EmailTemplateHelper.getHtmlDivText(properties.getProperty("NoPicturesRememberText2"))+EmailTemplateHelper.getHtmlSearchFaces()+EmailTemplateHelper.getHtmlDivText(properties.getProperty("NoPicturesRememberText3"))+EmailTemplateHelper.getHtmlDivText(properties.getProperty("NoPicturesRememberText4"))+EmailTemplateHelper.getHtmlDivText(properties.getProperty("NoPicturesRememberText5"))+EmailTemplateHelper.getSocialNetworkFooter();
        List<IdDataSource> dataSources=new ArrayList<IdDataSource>();
        dataSources.add(new IdDataSource(EmailTemplateHelper.getFileDataSourceFromURL(Thread.currentThread().getContextClassLoader().getResource("emailLogo.png")), logoId));
        dataSources.add(new IdDataSource(EmailTemplateHelper.getFileDataSourceFromURL(Thread.currentThread().getContextClassLoader().getResource("carasBusqueda.jpg")), facesSearchId));
        emailEJB.sendHtmlMail(candidate.getUser().getEmail(), subject, html, dataSources);
    }  
    
    public String getHtmlWelcomeBody() throws IOException{
        StringBuilder sb=new StringBuilder();
        sb.append("<div style=\"padding-bottom:25px;\"><span style=\"font-family:Helvetica,Arial,sans-serif;font-size:28px;font-weight:700;color:#3E3E3E;padding-bottom:25px;\">");
        sb.append(EmailTemplateHelper.getMessages().getString("WelcomeToTheRecruitery"));
        sb.append("</span></div>");
        sb.append("<div style=\"padding-bottom:25px;\"><span style=\"font-family:Arial,Helvetica,sans-serif;font-size:13px;color:#868585;margin:0;padding:0;line-height:1.6em\">");
        sb.append(EmailTemplateHelper.getMessages().getString("WelcomeToTheRecruiteryEmailText"));
        sb.append("</span></div>");
        return sb.toString();
    }
    
        
    public String getHtmlUserCredentials(String user, String password) throws IOException{
        StringBuilder sb=new StringBuilder();
        sb.append("<div style=\"padding-bottom:10px;\"><span style=\"font-family:Arial,Helvetica,sans-serif;font-size:13px;color:#868585;margin:0;padding:0;line-height:1.6em\">");
        sb.append(EmailTemplateHelper.getMessages().getString("User"));
        sb.append(": ");
        sb.append(user);
        sb.append("</span></div>");
        return sb.toString();
    }
    
    public String getHtmlFacesBody(){
        StringBuilder sb=new StringBuilder();
        sb.append("<div style=\"padding-bottom:25px;\"><img src=\"cid:faces\"></img></div>");
        return sb.toString();
    }
    
    public  DataSource getLogoDataSource(){
        return EmailTemplateHelper.getFileDataSource(logoRelativePath);
    }

    
    public static DataSource getFacesDataSource(){
        return EmailTemplateHelper.getFileDataSource(facesRelativePath);
    }
    

      
            
}
